package com.bjpower.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpower.convent.UserPageInfoVoConvent;
import com.bjpower.domain.UserInfo;
import com.bjpower.domain.UserPayInfo;
import com.bjpower.models.Result;
import com.bjpower.models.request.MerchINfoRequest;
import com.bjpower.models.request.UserPageInfoRequest;
import com.bjpower.models.vo.UserPageInfoVo;
import com.bjpower.services.UserService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private UserPageInfoVoConvent userPageInfoVoConvent;
    /**
     * 分页查询用户信息
     * @return
     */
    @GetMapping("/user/list")
    public Result getUserInfo(UserPageInfoRequest userPageInfoRequest) {
        IPage<UserInfo> userInfoList = userService.getUserInfoList(userPageInfoRequest);
        //从分页对象中获取集合对象
        List<UserInfo> pageList = userInfoList.getRecords();
        //获取分页的总页数
        long pages = userInfoList.getPages();
        //获取当前页码
        long current = userInfoList.getCurrent();
        //获取总记录数
        long total = userInfoList.getTotal();
        //添加MapStrut结果及映射对返回数据进行处理
        List<UserPageInfoVo> list = userPageInfoVoConvent.toUserPageInfoVoList(pageList);
        //将分页信息存储到map集合中
        HashMap<String, Object> data = new HashMap<>();
        data.put("list", list);
        data.put("pages", pages);
        data.put("current", current);

        return Result.ok(data);
    }

    @PostMapping("/update/merch/info")
    public Result insertMerchInfo(@RequestBody @Validated MerchINfoRequest merchINfoRequest){
        userService.insertMerchInfo(merchINfoRequest);
        return Result.ok();
    }
    /**
     * 查询用户信息
     */
    @GetMapping("/merch/info")
    public Result getMerchInfo(String userNo){
        UserInfo userInfo = userService.getUserInfoByUserNo(userNo);
        UserPageInfoVo userPageInfoVo = userPageInfoVoConvent.toUserPageInfoVo(userInfo);
        return Result.ok(userPageInfoVo);
    }
    /**
     * 根据用户名称模糊查询用户列表
     */
    @GetMapping("/merch/shopList")
    public Result getMerchShopNameList(String shopName){
        //判断用户输入信息是否为空
        if(StrUtil.isNotBlank(shopName)){
            List<UserInfo> userInfoList = userService.getMerchShopNameList(shopName);
            ArrayList<UserPageInfoVo> UserInfoVoList = new ArrayList<>();
            for (UserInfo userInfo : userInfoList) {
                UserPageInfoVo userPageInfoVo = new UserPageInfoVo();
                userPageInfoVo.setShopName(userInfo.getShopName());
                userPageInfoVo.setUserNo(userInfo.getUserNo());
                UserInfoVoList.add(userPageInfoVo);
            }
            return Result.ok(UserInfoVoList);
        }
        return Result.ok();
    }
}
